package dav.mobile.garuda.states;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actors.Button;
import com.badlogic.gdx.scenes.scene2d.actors.Button.ClickListener;
import com.badlogic.gdx.scenes.scene2d.actors.Image;

import dav.mobile.garuda.components.Constants;
import dav.mobile.garuda.components.Profile;
import dav.mobile.garuda.components.ResourceManager;
import dav.mobile.garuda.components.SoundManager;
import dav.mobile.garuda.states.StateMap.MapMode;

public class StateMenu extends State implements InputProcessor {
	//Menu Stage
	Stage m_Stage;
	
	Image m_Background; 
	Image m_Title;
	final Button m_PlayButton;
	final Button m_FreePlayButton;
	final Button m_CreditsButton;
	final Button m_SoundButton;
	
	public StateMenu() {
		//Load game profile
		
		//Create Stage
		m_Stage = new Stage(Constants.GAME_WIDTH, Constants.GAME_HEIGHT, true);
	
		//Load background
		m_Background = new Image("Menu Background", ResourceManager.m_MenuBackground);
		m_Stage.addActor(m_Background);
		
		//Load Title
		m_Title = new Image("Menu Title", ResourceManager.m_MenuTitle);
		m_Title.x = (Constants.GAME_WIDTH - Constants.MENU_TITLE_WIDTH) / 2;
		m_Title.y = (Constants.GAME_HEIGHT - Constants.MENU_TITLE_HEIGHT) / 2 + 75;
		m_Stage.addActor(m_Title);
		
		//Play button
		m_PlayButton = new Button("Menu Play", ResourceManager.m_MenuPlay, ResourceManager.m_MenuPlayPressed);
		m_PlayButton.x = (Constants.GAME_WIDTH - Constants.MENU_PLAY_WIDTH) / 2 - 120;
		m_PlayButton.y = (Constants.GAME_HEIGHT - Constants.MENU_PLAY_HEIGHT) / 2 - 40;
		m_PlayButton.clickListener = new ClickListener() {
			
			@Override
			public void clicked(Button arg0) {
				if (Profile.getInstance().hasGameInit()) {
					//Play game from map
					SetState(new StateMap(MapMode.QUEST));
				} else {
					//Init game
					Profile.getInstance().InitGame();
					
					//Enter epilogue
					SetState(new StateScene("prolog-1"));
				}
			}
		};
		m_Stage.addActor(m_PlayButton);
		
		//Free play button
		m_FreePlayButton = new Button("Menu Free Play", ResourceManager.m_MenuFreePlay, ResourceManager.m_MenuFreePlayPressed);
		m_FreePlayButton.x = (Constants.GAME_WIDTH - Constants.MENU_PLAY_WIDTH) / 2 + 15;
		m_FreePlayButton.y = (Constants.GAME_HEIGHT - Constants.MENU_PLAY_HEIGHT) / 2 - 36;
		m_FreePlayButton.clickListener = new ClickListener() {
			
			@Override
			public void clicked(Button arg0) {
				//SetState(new StateMap(MapMode.FREE));
				SoundManager.PlaySFX(SoundManager.m_MenuSound);
			}
		};
		m_Stage.addActor(m_FreePlayButton);
		
		//Credits button
		m_CreditsButton = new Button("Menu Credits", ResourceManager.m_MenuCredits, ResourceManager.m_MenuCreditsPressed);
		m_CreditsButton.x = (Constants.GAME_WIDTH - Constants.MENU_PLAY_WIDTH) / 2 + 138;
		m_CreditsButton.y = (Constants.GAME_HEIGHT - Constants.MENU_PLAY_HEIGHT) / 2 - 32;
		m_CreditsButton.clickListener = new ClickListener() {
			
			@Override
			public void clicked(Button arg0) {
				SoundManager.PlaySFX(SoundManager.m_MenuSound);
			}
		};
		m_Stage.addActor(m_CreditsButton);

		//Sound button
		m_SoundButton = new Button("Menu Sound", ResourceManager.m_MenuSoundPressed);
		m_SoundButton.x = Constants.MENU_SOUND_X;
		m_SoundButton.y = Constants.MENU_SOUND_Y;
		m_SoundButton.clickListener = new ClickListener() {
			
			@Override
			public void clicked(Button arg0) {
				Constants.GAME_SOUND_ENABLED = !Constants.GAME_SOUND_ENABLED;
				
				if (Constants.GAME_SOUND_ENABLED) {
					m_SoundButton.unpressedRegion.setRegion(ResourceManager.m_MenuSoundPressed);
				}
				else m_SoundButton.unpressedRegion.setRegion(ResourceManager.m_MenuSound);
				
				SoundManager.StopBGM(SoundManager.m_MenuMusic);
				SoundManager.PlayBGM(SoundManager.m_MenuMusic);
				
				SoundManager.PlaySFX(SoundManager.m_MenuSound);
			}
		};
		m_Stage.addActor(m_SoundButton);
		
		//Play BGM
		SoundManager.PlayBGM(SoundManager.m_MenuMusic);
		
		//Set input processor
		Gdx.input.setInputProcessor(this);
	}
	
	@Override
	public void Update() {
		m_Stage.act(Gdx.graphics.getDeltaTime());
	}

	@Override
	public void Render() {
		m_Stage.draw();
	}
	
	@Override
	public void Dispose() {
		super.Dispose();
		
		SoundManager.StopBGM(SoundManager.m_MenuMusic);
		
		m_Stage.removeActor(m_Title);
		m_Stage.removeActor(m_Background);
		m_Stage.removeActor(m_PlayButton);
		m_Stage.removeActor(m_FreePlayButton);
		m_Stage.removeActor(m_CreditsButton);
		m_Stage.removeActor(m_SoundButton);
		
		m_Stage.dispose();
	}

	@Override
	public boolean keyDown(int keycode) {
		
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		m_Stage.touchDown(x, y, pointer, button);
		
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		m_Stage.touchUp(x, y, pointer, button);
		
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		m_Stage.touchDragged(x, y, pointer);
		
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		
		return false;
	}

}
